home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / samples / olympic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  8.7 KB  |  376 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #define _HPUX_SOURCE
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <math.h>
  30. #include <sys/types.h>
  31. #include <sys/time.h>
  32. #include "tk.h"
  33.  
  34. extern double drand48(void);
  35. extern void srand48(long seedval);
  36.  
  37.  
  38. #define XSIZE    100
  39. #define YSIZE    75
  40.  
  41. #define RINGS 5
  42. #define BLUERING 0
  43. #define BLACKRING 1
  44. #define REDRING 2
  45. #define YELLOWRING 3
  46. #define GREENRING 4
  47.  
  48. #define BACKGROUND 8
  49.  
  50. enum {
  51.     BLACK = 0,
  52.     RED,
  53.     GREEN,
  54.     YELLOW,
  55.     BLUE,
  56.     MAGENTA,
  57.     CYAN,
  58.     WHITE
  59. };
  60.  
  61.  
  62. GLenum rgb, doubleBuffer, directRender;
  63.  
  64. unsigned char rgb_colors[RINGS][3];
  65. int mapped_colors[RINGS];
  66. float dests[RINGS][3];
  67. float offsets[RINGS][3];
  68. float angs[RINGS];
  69. float rotAxis[RINGS][3];
  70. int iters[RINGS];
  71. GLuint theTorus;
  72.  
  73.  
  74. void FillTorus(float rc, int numc, float rt, int numt)
  75. {
  76.     int i, j, k;
  77.     double s, t;
  78.     double x, y, z;
  79.     double pi, twopi;
  80.  
  81.     pi = 3.14159265358979323846;
  82.     twopi = 2 * pi;
  83.  
  84.     for (i = 0; i < numc; i++) {
  85.     glBegin(GL_QUAD_STRIP);
  86.         for (j = 0; j <= numt; j++) {
  87.         for (k = 1; k >= 0; k--) {
  88.         s = (i + k) % numc + 0.5;
  89.         t = j % numt;
  90.  
  91.         x = cos(t*twopi/numt) * cos(s*twopi/numc);
  92.         y = sin(t*twopi/numt) * cos(s*twopi/numc);
  93.         z = sin(s*twopi/numc);
  94.         glNormal3f(x, y, z);
  95.  
  96.         x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  97.         y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  98.         z = rc * sin(s*twopi/numc);
  99.         glVertex3f(x, y, z);
  100.         }
  101.         }
  102.     glEnd();
  103.     }
  104. }
  105.  
  106. float Clamp(int iters_left, float t)
  107. {
  108.  
  109.     if (iters_left < 3) {
  110.     return 0.0;
  111.     }
  112.     return (iters_left-2)*t/iters_left;
  113. }
  114.  
  115. void DrawScene(void)
  116. {
  117.     int i, j;
  118.     GLboolean goIdle;
  119.  
  120.     goIdle = GL_TRUE;
  121.     for (i = 0; i < RINGS; i++) {
  122.     if (iters[i]) {
  123.         for (j = 0; j < 3; j++) {
  124.         offsets[i][j] = Clamp(iters[i], offsets[i][j]);
  125.         }
  126.         angs[i] = Clamp(iters[i], angs[i]);
  127.         iters[i]--;
  128.         goIdle = GL_FALSE;
  129.     }
  130.     }
  131.     if (goIdle) {
  132.        tkIdleFunc(NULL);
  133.     }
  134.  
  135.     glPushMatrix();
  136.     
  137.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  138.     gluLookAt(0,0,10, 0,0,0, 0,1,0);
  139.  
  140.     for (i = 0; i < RINGS; i++) {
  141.     if (rgb) {
  142.         glColor3ubv(rgb_colors[i]);
  143.     } else {
  144.         glIndexi(mapped_colors[i]);
  145.     }
  146.     glPushMatrix();
  147.     glTranslatef(dests[i][0]+offsets[i][0], dests[i][1]+offsets[i][1],
  148.              dests[i][2]+offsets[i][2]);
  149.     glRotatef(angs[i], rotAxis[i][0], rotAxis[i][1], rotAxis[i][2]);
  150.     glCallList(theTorus);
  151.     glPopMatrix();
  152.     }
  153.  
  154.     glPopMatrix();
  155.  
  156.     glFlush();
  157.     if (doubleBuffer) {
  158.     tkSwapBuffers();
  159.     }
  160. }
  161.  
  162. float MyRand(void)
  163. {
  164.  
  165.     return 10.0 * (drand48() - 0.5);
  166. }
  167.  
  168. void ReInit(void)
  169. {
  170.     int i;
  171.     float deviation;
  172.  
  173.     deviation = MyRand() / 2;
  174.     deviation = deviation * deviation;
  175.     for (i = 0; i < RINGS; i++) {
  176.     offsets[i][0] = MyRand();
  177.     offsets[i][1] = MyRand();
  178.     offsets[i][2] = MyRand();
  179.     angs[i] = 260.0 * MyRand();
  180.     rotAxis[i][0] = MyRand();
  181.     rotAxis[i][1] = MyRand();
  182.     rotAxis[i][2] = MyRand();
  183.     iters[i] = (deviation * MyRand() + 60.0);
  184.     }
  185.     tkIdleFunc(DrawScene);
  186. }
  187.  
  188. void Init(void)
  189. {
  190.     int gid;
  191.     float base, height;
  192.     float aspect, x, y;
  193.     int i;
  194.     struct timeval t;
  195.     struct timezone tz;
  196.     float sc = 10;
  197.     float top_y = 1.0;
  198.     float bottom_y = 0.0;
  199.     float top_z = 0.15;
  200.     float bottom_z = 0.69;
  201.     float spacing = 2.5;
  202.     static float lmodel_ambient[] = {0.0, 0.0, 0.0, 0.0};
  203.     static float lmodel_twoside[] = {GL_FALSE};
  204.     static float lmodel_local[] = {GL_FALSE};
  205.     static float light0_ambient[] = {0.1, 0.1, 0.1, 1.0};
  206.     static float light0_diffuse[] = {1.0, 1.0, 1.0, 0.0};
  207.     static float light0_position[] = {0.8660254, 0.5, 1, 0};
  208.     static float light0_specular[] = {1.0, 1.0, 1.0, 0.0};
  209.     static float bevel_mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  210.     static float bevel_mat_shininess[] = {40.0};
  211.     static float bevel_mat_specular[] = {1.0, 1.0, 1.0, 0.0};
  212.     static float bevel_mat_diffuse[] = {1.0, 0.0, 0.0, 0.0};
  213.  
  214.     gettimeofday(&t, &tz);
  215.     srand48(t.tv_usec);
  216.     ReInit();
  217.     for (i = 0; i < RINGS; i++) {
  218.     rgb_colors[i][0] = rgb_colors[i][1] = rgb_colors[i][2] = 0;
  219.     }
  220.     rgb_colors[BLUERING][2] = 255;
  221.     rgb_colors[REDRING][0] = 255;
  222.     rgb_colors[GREENRING][1] = 255;
  223.     rgb_colors[YELLOWRING][0] = 255;
  224.     rgb_colors[YELLOWRING][1] = 255;
  225.     mapped_colors[BLUERING] = BLUE;
  226.     mapped_colors[REDRING] = RED;
  227.     mapped_colors[GREENRING] = GREEN;
  228.     mapped_colors[YELLOWRING] = YELLOW;
  229.     mapped_colors[BLACKRING] = BLACK;
  230.  
  231.     dests[BLUERING][0] = -spacing;
  232.     dests[BLUERING][1] = top_y;
  233.     dests[BLUERING][2] = top_z;
  234.  
  235.     dests[BLACKRING][0] = 0.0;
  236.     dests[BLACKRING][1] = top_y;
  237.     dests[BLACKRING][2] = top_z;
  238.  
  239.     dests[REDRING][0] = spacing;
  240.     dests[REDRING][1] = top_y;
  241.     dests[REDRING][2] = top_z;
  242.  
  243.     dests[YELLOWRING][0] = -spacing / 2.0;
  244.     dests[YELLOWRING][1] = bottom_y;
  245.     dests[YELLOWRING][2] = bottom_z;
  246.  
  247.     dests[GREENRING][0] = spacing / 2.0;
  248.     dests[GREENRING][1] = bottom_y;
  249.     dests[GREENRING][2] = bottom_z;
  250.  
  251.     base = 2.0; 
  252.     height = 2.0;
  253.     theTorus = glGenLists(1);
  254.     glNewList(theTorus, GL_COMPILE);
  255.     FillTorus(0.1, 8, 1.0, 25);
  256.     glEndList();
  257.  
  258.     x = (float)XSIZE;
  259.     y = (float)YSIZE;
  260.     aspect = x / y;
  261.     glEnable(GL_CULL_FACE);
  262.     glCullFace(GL_BACK);
  263.     glEnable(GL_DEPTH_TEST);
  264.     glClearDepth(1.0);
  265.  
  266.     if (rgb) {
  267.     glClearColor(0.5, 0.5, 0.5, 0.0);
  268.     glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  269.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  270.     glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
  271.     glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  272.     glEnable(GL_LIGHT0);
  273.  
  274.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local);
  275.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  276.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  277.     glEnable(GL_LIGHTING);
  278.  
  279.     glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient);
  280.     glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess);
  281.     glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular);
  282.     glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse);
  283.  
  284.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  285.     glEnable(GL_COLOR_MATERIAL);
  286.     glShadeModel(GL_SMOOTH);
  287.     } else {
  288.     glClearIndex(BACKGROUND);
  289.     glShadeModel(GL_FLAT);
  290.     }
  291.  
  292.     glMatrixMode(GL_PROJECTION);
  293.     gluPerspective(45, 1.33, 0.1, 100.0);
  294.     glMatrixMode(GL_MODELVIEW);
  295. }
  296.  
  297. void Reshape(int width, int height)
  298. {
  299.  
  300.     glViewport(0, 0, width, height);
  301. }
  302.  
  303. GLenum Key(int key, GLenum mask)
  304. {
  305.  
  306.     switch (key) {
  307.       case TK_ESCAPE:
  308.     tkQuit();
  309.       case TK_SPACE:
  310.     ReInit();
  311.     break;
  312.       default:
  313.     return GL_FALSE;
  314.     }
  315.     return GL_TRUE;
  316. }
  317.  
  318. GLenum Args(int argc, char **argv)
  319. {
  320.     GLint i;
  321.  
  322.     rgb = GL_TRUE;
  323.     doubleBuffer = GL_FALSE;
  324.     directRender = GL_TRUE;
  325.  
  326.     for (i = 1; i < argc; i++) {
  327.     if (strcmp(argv[i], "-ci") == 0) {
  328.         rgb = GL_FALSE;
  329.     } else if (strcmp(argv[i], "-rgb") == 0) {
  330.         rgb = GL_TRUE;
  331.     } else if (strcmp(argv[i], "-sb") == 0) {
  332.         doubleBuffer = GL_FALSE;
  333.     } else if (strcmp(argv[i], "-db") == 0) {
  334.         doubleBuffer = GL_TRUE;
  335.     } else if (strcmp(argv[i], "-dr") == 0) {
  336.         directRender = GL_TRUE;
  337.     } else if (strcmp(argv[i], "-ir") == 0) {
  338.         directRender = GL_FALSE;
  339.     } else {
  340.         printf("%s (Bad option).\n", argv[i]);
  341.         return GL_FALSE;
  342.     }
  343.     }
  344.     return GL_TRUE;
  345. }
  346.  
  347. void main(int argc, char **argv)
  348. {
  349.     GLenum type;
  350.  
  351.     if (Args(argc, argv) == GL_FALSE) {
  352.     tkQuit();
  353.     }
  354.  
  355.     tkInitPosition(0, 0, 400, 300);
  356.  
  357.     type = TK_DEPTH;
  358.     type |= (rgb) ? TK_RGB : TK_INDEX;
  359.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  360.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  361.     tkInitDisplayMode(type);
  362.  
  363.     if (tkInitWindow("Olympic") == GL_FALSE) {
  364.         tkQuit();
  365.     }
  366.  
  367.     Init();
  368.  
  369.     tkExposeFunc(Reshape);
  370.     tkReshapeFunc(Reshape);
  371.     tkKeyDownFunc(Key);
  372.     tkIdleFunc(DrawScene);
  373.  
  374.     tkExec();
  375. }
  376.